package examples;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @author Jay
 */
public class CountDownLatchExample1 {

    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(), new ThreadFactory() {
            private int id = 0;
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "matching-thread-" + id++);
            }
        });
        int threadCount = 10;
        String[] threadStatus = new String[threadCount];
        for(int i = 0; i < threadCount; i++){
            int id = i;
            executor.submit(()->{
                for(int j = 1; j <= 100; j++){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    threadStatus[id] = "" + j + "%";
                    System.out.print("\r" + Arrays.toString(threadStatus));
                }
                countDownLatch.countDown();
            });
        }

        try {
            countDownLatch.await();
            executor.shutdown();
            System.out.println("\ndone");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
